package com.qf.myblogssm.controller;

import com.qf.myblogssm.pojo.Message;
import com.qf.myblogssm.pojo.Tarticle;
import com.qf.myblogssm.pojo.Ttag;
import com.qf.myblogssm.pojo.Tuser;
import com.qf.myblogssm.service.ArticleService;
import com.qf.myblogssm.service.LoginService;
import com.qf.myblogssm.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * create by 王镜 on 2019/6/14 0014 16:25
 */
//注解，表示当前层为控制层
@Controller
@RequestMapping("/users")
public class UserController {

    //自动装配
    @Autowired
    // @Resource都是实现自动装配的功能
    private LoginService loginService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private TagService tagService;

    //restful了解一下
    @RequestMapping("/ni")
    public void login() {
        //  Integer id=1;
        String name = "lonely";
        String password = "123";
        Tuser tuser = loginService.login(name);
        if (tuser != null) {
            //判断密码是否正确
            if (password.equals(tuser.getUserPassword())) {
                System.out.println("欢迎你" + tuser.getUserName());
            } else {
                System.out.println("登录失败!");
            }
        }
    }

    @RequestMapping("/loginOut")
    public String deleteByid(HttpServletRequest request) {
        Enumeration em = request.getSession().getAttributeNames();
        while(em.hasMoreElements()){
            request.getSession().removeAttribute(em.nextElement().toString());
        }
        return "login";
    }

    @RequestMapping("/ajax")
    //将我们的数据转换为json格式，避免视图解析器给我们返回的数据添加上路径格式
    @ResponseBody
    public Message ajax() {
        Message message = new Message();
        message.setMsg("hhhhhhh");
        return message;
    }

    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        //先从域中获取对象
        Tuser user = (Tuser) request.getSession().getAttribute("user");
        if (user != null) {
            //我们在域中添加user对象时，会给session设置过期时间
            //有一个radis的nosql的数据库，会存储我们的user值，会通过uuId的函数生成一个token
            request.getSession().setAttribute("user", user);
            //初始化主页面的信息
            initMyblog(request);
            return "main";
        } else {
            return "login";
        }
    }

    @RequestMapping("/login")
    //第一种方式：把前端jsp页面和pojo中实体类名字相同，会直接封装成一个对象
//    public String login(Tuser user)
//    {
//        System.out.println(user.getUserName());
//
//    }
    public String login(String userName, String userPassword, HttpServletRequest request) {
        Tuser tuser = loginService.login(userName, userPassword);
        if (tuser != null) {
            request.getSession().setAttribute("user", tuser);
            //初始化主页面数据
            initMyblog(request);
            return "main";
        } else {
            request.setAttribute("msg", "用户名或者密码错误");
            return "login";
        }
    }
//    @RequestMapping("/getById")
//    public String getArticleById(Integer id, Model model){
//        return null;
//    }
    public void initMyblog(HttpServletRequest request) {
       List<Tarticle> article_list=articleService.selectAllArticles();
       request.setAttribute("article_list",article_list);

       List<Ttag> tag_ist=tagService.selectAllTags();
       request.setAttribute("tag_list",tag_ist);

       Integer number=articleService.countNumber();
       request.setAttribute("article_number",number);

       Integer sortnumber=articleService.countSortNumber();
       request.setAttribute("sort_number",sortnumber);

       Integer tagnumber=tagService.countTagNumber();
        request.setAttribute("tag_number",tagnumber);

        List<Tarticle> tarticles=articleService.getArticle_sort_count();//获取文章的分类个数
        Map<String,Integer> map=new HashMap();
        for(Tarticle tarticle:tarticles){
            map.put(tarticle.getSort(),tarticle.getCounts());
        }
        request.setAttribute("sort_count_map",map);
    }
}